package com.moyq5.permit.center.web.controller;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.moyq5.mvc.framework.general.Body;
import com.moyq5.mvc.framework.general.PageData;
import com.moyq5.mvc.framework.jackson.result.ObjectResult;
import com.moyq5.mvc.framework.jackson.result.PageResult;
import com.moyq5.permit.center.bean.entity.Charge;
import com.moyq5.permit.center.bean.search.Charge4Search;
import com.moyq5.permit.center.feign.client.ChargeFeign;
import com.moyq5.permit.center.web.jackson.filter.ChargeFilter;


/**
 * @author Moyq5
 * @date 2019年7月31日
 */
@RestController
@RequestMapping("/charge")
public class ChargeController extends BaseController {
	
	private static Logger log = LoggerFactory.getLogger(ChargeController.class);
	
	@Autowired
	private ChargeFeign chargeFeign;
	
	@InitBinder
	protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
		DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
		CustomDateEditor editor = new CustomDateEditor(df, true);// true表示允许为空，false反之
		binder.registerCustomEditor(Date.class, editor);
	}

	
	@GetMapping(value="/page", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
	@ResponseBody
	public String page(Charge4Search charge4s, PageData pd) throws JsonProcessingException {
		log.debug("查询充值信息>>>page(Charge4Search, PageData)");
		ObjectResult<BigDecimal> amountRst = chargeFeign.sumAmount((new Body<Charge4Search, String>(charge4s, pd.getSearchWord())));
		PageResult<Charge> pageRst = chargeFeign.page(new Body<Charge4Search, PageData>(charge4s, pd));
		Map<String, Object> map = new HashMap<>();
		map.put("sumAmount", amountRst.getData());
		map.put("page", pageRst.getData());
		ObjectResult<Map<String, Object>> result = new ObjectResult<>();
		result.setOk();
		result.setData(map);
		return toJson(result, ChargeFilter.get());
	}
	
}
